package com.sunmi.paymentdemo.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;

import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.sunmi.paymentdemo.R;

public class WaitingDialog extends Dialog implements View.OnClickListener {
    private QMUILoadingView qmuiLoadingView;
    private QMUIRoundButton cancelBtn;
    private DialogListener dialogListener;

    public WaitingDialog(@NonNull Context context) {
        super(context, R.style.defaultDialogStyle);
        initDialog();
    }

    public void setDialogListener(DialogListener dialogListener) {
        this.dialogListener = dialogListener;
    }

    public void setCancelBtnShow(boolean isShow) {
        if (isShow) {
            cancelBtn.setVisibility(View.VISIBLE);
        } else {
            cancelBtn.setVisibility(View.GONE);
        }
    }

    public interface DialogListener {
        void onCancel();
    }

    private void initDialog() {
        setContentView(R.layout.wait_response_dialog);
        // 居中
        qmuiLoadingView = findViewById(R.id.empty_view_loading);
        cancelBtn = findViewById(R.id.btn_cancel);
        cancelBtn.setOnClickListener(this);
        getWindow().getAttributes().gravity = Gravity.CENTER;
//        setCancelable(false);
        setCanceledOnTouchOutside(false);
        qmuiLoadingView.setVisibility(View.VISIBLE);
        qmuiLoadingView.setColor(R.color.qmui_config_color_black);
        qmuiLoadingView.setActivated(true);
        qmuiLoadingView.setSize(100);
        qmuiLoadingView.start();
    }

    @Override
    public void onClick(View v) {
        if (dialogListener != null) {
            dialogListener.onCancel();
        }
        qmuiLoadingView.stop();
        dismiss();
    }
}